<?php

namespace Application\Service;

use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;

class MailService
{

    public function envoyer_mail_copie ( $config_vars, $content ) {
        $config_contact = $config_vars['contact'];

        // make a header as html  
        $html = new MimePart($content);  
        $html->type = "text/html";  
        $body = new MimeMessage();  
        $body->setParts(array($html,));

        $mail = new Mail\Message();
        $mail->setBody( $body );

        $index = 0;

        foreach ($config_contact as $key => $value) {
            $libelleMailTo = $key;
            $adresseMailTo = $value;

            if( $index == 0 ) {
                $mail->setFrom( $adresseMailTo, $libelleMailTo );
            }
            
            $mail->addTo( $adresseMailTo, $libelleMailTo );

            $index++;
        }
        
        // $mail->addTo( "nicolas.sesma@1001treks.com", $libelleMailTo );

        $mail->setSubject('[Copie] Demande de renseignement pour le séjour');

        $transport = new Mail\Transport\Sendmail();
        $transport->send($mail);
    }

    public function envoyer_demande_renseignement ( $detailSejour, $detailAgence, $contact_datas, $config_vars, $content ) {
        $emailFrom = $contact_datas->mailInfo; /* mail de la personne qui demande un renseignement */
        $comment = $contact_datas->textInfo;

        // make a header as html  
        $html = new MimePart($content);  
        $html->type = "text/html";  
        $body = new MimeMessage();  
        $body->setParts(array($html,));

        $mail = new Mail\Message();
        $mail->setBody( $body );
        $mail->setFrom( $emailFrom, $emailFrom );

        $mail->addTo( $detailAgence->mail, $detailAgence->libelle );

        $mail->setSubject('Demande de renseignement pour le séjour ' . $detailSejour->titre . " - " . $detailSejour->libellePays );

        $transport = new Mail\Transport\Sendmail();
        $transport->send($mail);
    }

    public function envoyer_demande_contact ( $contact, $config_contact ) {

        $libelleMailTo = key($config_contact);
        $adresseMailTo = $config_contact[ key($config_contact) ];
        
        $emailFrom = $contact->mailContact; /* mail de la personne qui demande un renseignement */
        $comment = $contact->textContact;
        
        // make a header as html  
        $html = new MimePart( nl2br( $comment ) );  
        $html->type = "text/html";  
        $body = new MimeMessage();  
        $body->setParts(array($html,));

        $mail = new Mail\Message();
        $mail->setBody( $body );
        $mail->setFrom( $emailFrom, $emailFrom );

        $mail->addTo( $adresseMailTo, $libelleMailTo );
        
        $mail->setSubject('Demande de contact 1001 treks');

        $transport = new Mail\Transport\Sendmail();
        // $transport = new Mail\Transport\Smtp($options);
        $transport->send($mail);
    }

}